package com.ruoyi.thirdpart.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.thirdpart.domain.MapLocation;
import com.ruoyi.thirdpart.service.IMapLocationService;
import com.ruoyi.utils.HttpUtils;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 地图 逆地理解析 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class MapLocationServiceImpl implements IMapLocationService
{
    private String appkey = "9e9832c47fea8e1cbc220a7302919248";
    private String ver = "1";
    // lon	坐标的x值	string	是
    // lat	坐标的y值	string	是
    // appkey	网站的唯一编码	string	是
    // ver	接口版本	string	是
    // 逆地理编码查询  http://lbs.tianditu.gov.cn/server/geocoding.html
    // 接口实例 http://api.tianditu.gov.cn/geocoder?postStr={'lon':116.37304,'lat':39.92594,'ver':1}&type=geocode&tk=您的密钥
    // http://api.tianditu.gov.cn/geocoder?postStr={%27lon%27:116.37304,%27lat%27:39.92594,%27ver%27:1}&type=geocode&tk=9e9832c47fea8e1cbc220a7302919248
    @Override
    public MapLocation geocoder(String lon, String lat){
        String postStr = "{'lon':" + lon + ",'lat':" + lat + ",'ver':1}";
        try {
            postStr = URLEncoder.encode(postStr, "UTF-8");
        }catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        String url = "http://api.tianditu.gov.cn/geocoder?postStr=" + postStr +
                "&type=geocode" +
                "&tk=" + appkey;
        Map<String, String> requestObject1 = new HashMap<String, String>();
        JSONObject resultObject = HttpUtils.doGet(url, requestObject1);
        System.out.println("========= geocoder resultObject ==========");
        System.out.println(resultObject);
        String result = resultObject.getString("result");
        Map<String, Object> map1 = JSON.parseObject(result, new TypeReference<Map<String, Object>>(){});
        MapLocation mapLocation = new MapLocation();
        String locationStr = map1.get("location").toString();
        Map<String, Object> location = JSON.parseObject(locationStr, new TypeReference<Map<String, Object>>(){});
        String addressComponentStr = map1.get("addressComponent").toString();
        Map<String, Object> addressComponent = JSON.parseObject(addressComponentStr, new TypeReference<Map<String, Object>>(){});
        mapLocation.setCity(result);
        mapLocation.setLat(location.get("lat").toString());
        mapLocation.setLon(location.get("lon").toString());
        mapLocation.setProvince(addressComponent.get("province").toString());
        mapLocation.setProvinceCode(addressComponent.get("province_code").toString());
        mapLocation.setCity(addressComponent.get("city").toString());
        mapLocation.setCityCode(addressComponent.get("city_code").toString());
        mapLocation.setCounty(addressComponent.get("county").toString());
        mapLocation.setCountyCode(addressComponent.get("county_code").toString());
        mapLocation.setTown(addressComponent.get("town").toString());
        mapLocation.setTownCode(addressComponent.get("town_code").toString());
        mapLocation.setRoad(addressComponent.get("road").toString());
        mapLocation.setFormattedAddress(map1.get("formatted_address").toString());
        return mapLocation;
    }

}
